package com.example.netcamerastreamdemo

import android.Manifest
import android.media.MediaExtractor
import android.media.MediaPlayer
import android.os.Bundle
import android.util.Log
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream

class MediaDataSourceTestActivity : AppCompatActivity(),
    MediaPlayer.OnErrorListener, MediaPlayer.OnPreparedListener,
    SurfaceHolder.Callback, MediaPlayer.OnCompletionListener{
    private val TAG = "MediaDataSourceTestActivity"

    lateinit var mStreamMediaDataSource : UrlMediaDataSource
    lateinit var mMediaPlayer : MediaPlayer
    var file: File? = null
    var fileOutputStream: FileOutputStream? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_player_test)
        mMediaPlayer = MediaPlayer()
        val surfaceView = findViewById<SurfaceView>(R.id.surface_view)
        surfaceView.holder.addCallback(this)
        val msgData = VideoStreamPlayer.MsgData()
        msgData.dev_guid = "63-00F628C3B402-9A2A"
        msgData.video_chnn = 9
        msgData.sub_chnn = 2
        msgData.rec_path = "63-00F628C3B402-9A2A\\\\1623202389634\\\\63-00F628C3B402-9A2A_9_1623202389634"

//        mStreamMediaDataSource = StreamMediaDataSource(
//            this,
//            surfaceView,
////            "wss://shike-qa.video110.cn/nsdk",
//            "ws://192.168.0.139:8080/ws",
//            msgData
//        )

//        mStreamMediaDataSource.setmStreamCallback {
//            Log.i(TAG, "---StreamIsStart===")
//            mMediaPlayer.prepareAsync()
//        }

        mStreamMediaDataSource = UrlMediaDataSource("https://v-cdn.zjol.com.cn/280443.mp4")

        mMediaPlayer.setDataSource(mStreamMediaDataSource)

//        val path = getExternalFilesDir(null).toString()+ "/MV/flvtest.flv";
        val path = getExternalFilesDir(null).toString() + "/1623288807379.h264"

//        mMediaPlayer.setDataSource(path)
        mMediaPlayer.prepareAsync()

//        mMediaPlayer.setDataSource("https://v-cdn.zjol.com.cn/280443.mp4")
//        mMediaPlayer.prepareAsync()

        mMediaPlayer.setOnPreparedListener(this)
        mMediaPlayer.setOnErrorListener(this)
        mMediaPlayer.setOnCompletionListener(this)

        val prepareBtn = findViewById<Button>(R.id.prepare_btn)
        val playBtn = findViewById<Button>(R.id.play_btn)
        val stopBtn = findViewById<Button>(R.id.stop_btn)
        val testBtn = findViewById<Button>(R.id.test_btn)

//        mStreamMediaDataSource.prepare()

        prepareBtn.setOnClickListener {
//            mStreamMediaDataSource.startPlay()
        }
        playBtn.setOnClickListener {
            mMediaPlayer.start()
        }
        stopBtn.setOnClickListener {
            mMediaPlayer.stop()
//            mStreamMediaDataSource.stopPlay()
        }
        testBtn.setOnClickListener {
            val m = MediaExtractor()
            m.setDataSource(mStreamMediaDataSource)
            val f = m.getTrackFormat(0)
            Log.i(TAG, "---format===${f.toString()}")
        }
        requestPermissions(arrayOf(
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
        ), 0);

    }

    override fun onError(p0: MediaPlayer?, p1: Int, p2: Int): Boolean {
        Log.e(TAG, "---onError=p1=$p1-p2=$p2")
        return false
    }

    override fun onPrepared(p0: MediaPlayer?) {
        Log.i(TAG, "---onPrepared===")
//        mMediaPlayer.start()
    }

    override fun surfaceCreated(p0: SurfaceHolder) {
        mMediaPlayer.setSurface(p0.surface)
    }

    override fun surfaceChanged(p0: SurfaceHolder, p1: Int, p2: Int, p3: Int) {

    }

    override fun surfaceDestroyed(p0: SurfaceHolder) {

    }

    override fun onCompletion(p0: MediaPlayer?) {
        Log.i(TAG, "---onCompletion===")

    }
}